创建时间: | 2018/4/27 22:35 |
标签: | 微信 |
来源: | https://m.aliyun.com/doc/document_detail/52274.html |
在工作之中,由于SQL问题导致的数据库故障层出不穷,索引问题是SQL问题中出现频率最高的,常见的索引问题包括:无索引,隐式转换,索引创建不合理。
当数据库中出现访问表的SQL没创建索引导致全表扫描,如果表的数据量很大扫描大量的数据,执行效率过慢,占用数据库连接,连接数堆积很快达到数据库的最大连接数设置,新的应用请求将会被拒绝导致故障发生。
隐式转换是指SQL查询条件中的传入值与对应字段的数据定义不一致导致索引无法使用。常见隐式转换如字段的表结构定义为字符类型,但SQL传入值为数字;或者是字段定义collation为区分大小写,在多表关联的场景下,其表的关联字段大小写敏感定义各不相同。隐式转换会导致索引无法使用,进而出现上述慢SQL堆积数据库连接数跑满的情况。
查看表结构。
mysql> show create table customers;
CREATE TABLE `customers`(
`cust_id`int(11) NOT NULL AUTO_INCREMENT,
`cust_name`char(50) NOT NULL,
`cust_address`char(50) DEFAULT NULL,
`cust_city`char(50) DEFAULT NULL,
`cust_state`char(5) DEFAULT NULL,
`cust_zip`char(10) DEFAULT NULL,
`cust_country`char(50) DEFAULT NULL,
`cust_contact`char(50) DEFAULT NULL,
`cust_email`char(255) DEFAULT NULL,
PRIMARY KEY (`cust_id`),
) ENGINE=InnoDB AUTO_INCREMENT=10006 DEFAULT CHARSET=utf8
执行语句。
mysql>select*from customers where cust_zip ='44444' limit 0,1 \G;
执行计划。
mysql> explain select*from customers where cust_zip ='44444' limit 0,1 \G;
id:1
select_type: SIMPLE
table: customers
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows:505560
Extra:Using where
执行计划看到type为ALL,是全表扫描,每次执行需要扫描505560行数据,这是非常消耗性能的,那么下面将介绍优化方式。
添加索引。
mysql> alter table customers add index idx_cus(cust_zip);
执行计划。
mysql> explain select*from customers where cust_zip ='44444' limit 0,1 \G;
id:1
select_type: SIMPLE
table: customers
type: ref
possible_keys: idx_cus
key: idx_cus
key_len:31
ref: const
rows:4555
Extra:Using index condition
执行计划看到type为ref,基于索引的等值查询,或者表间等值连接。
表结构同上案例相同,执行语句。
mysql>select cust_id,cust_name,cust_zip from customers where cust_zip ='42222'order by cust_zip,cust_name;
执行计划。
mysql> explain select cust_id,cust_name,cust_zip from customers where cust_zip ='42222'order by cust_zip,cust_name\G;
id:1
select_type: SIMPLE
table: customers
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows:505560
Extra:Using filesort
添加索引。
mysql> alter table customers add index idx_cu_zip_name(cust_zip,cust_name);
执行计划。
mysql> explain select cust_id,cust_name,cust_zip from customers where cust_zip ='42222'order by cust_zip,cust_name\G;
id:1
select_type: SIMPLE
table: customers
type: ref
possible_keys: idx_cu_zip_name
key: idx_cu_zip_name
key_len:31
ref: const
rows:4555
Extra:Using where;Using index
order by使用字段,而且字段应该是索引字段。
mysql> explain select*from customers where cust_zip =44444 limit 0,1 \G;
id:1
select_type: SIMPLE
table: customers
type: ALL
possible_keys: idx_cus
key: NULL
key_len: NULL
ref: NULL
rows:505560
Extra:Using where
mysql> show warnings;
Warning:Cannotuse range access on index 'idx_cus' due to type or collation conversion on field 'cust_zip'
上述案例中由于表结构定义cust_zip字段是字符串数据类型,而应用传入的是数字,导致了隐式转换,无法使用索引。
查看表结构。
mysql> show create table customers1;
CREATE TABLE `customers1`(
`cust_id` varchar(10) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
`cust_name`char(50) NOT NULL,
KEY `idx_cu_id`(`cust_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql> show create table customers2;
CREATE TABLE `customers2`(
`cust_id` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`cust_name`char(50) NOT NULL,
KEY `idx_cu_id`(`cust_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
执行语句。
mysql>select customers1.*from customers2 left join customers1 on customers1.cust_id=customers2.cust_id where customers2.cust_id='x';
执行计划。
mysql> explain select customers1.*from customers2 left join customers1 on customers1.cust_id=customers2.cust_id where customers2.cust_id='x'\G;
***************************1. row ***************************
id:1
select_type: SIMPLE
table: customers2
type: ref
possible_keys: idx_cu_id
key: idx_cu_id
key_len:33
ref: const
rows:1
Extra:Using where;Using index
***************************2. row ***************************
id:1
select_type: SIMPLE
table: customers1
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows:1
Extra:Using where;Using join buffer (BlockNestedLoop)
修改COLLATE。
mysql> alter table customers1 modify column cust_id varchar(10) COLLATE utf8_bin ;
执行计划。
mysql> explain select cust_id,cust_name,cust_zip from customers where cust_zip ='42222'order by cust_zip,cust_name\G;
id:1
select_type: SIMPLE
table: customers2
type: ref
possible_keys: idx_cu_id
key: idx_cu_id
key_len:33
ref: const
rows:1
Extra:Using where;Using index
id:1
select_type: SIMPLE
table: customers1
type: ref
possible_keys: idx_cu_id
key: idx_cu_id
key_len:33
ref: const
rows:1
Extra:Using where
字段的COLLATE一致后执行计划使用到了索引,所以一定要注意表字段的collate属性的定义保持一致。
在使用索引时,我们可以通过explain查看SQL的执行计划,判断是否使用了索引以及发生了隐式转换,创建合适的索引。索引太复杂,创建需谨慎。